package com.polaris.live.im.db.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.im.sfs.bean.chat.ext.SingleMessageExtBean
import com.polaris.live.im.sfs.bean.chat.ext.SingleMessageParameterBean
import com.polaris.live.im.sfs.bean.chat.ext.SingleMessagePayloadBean

/**
 * 消息表
 *
 * @author Created by 半仙 on 2023/8/22/022 16:32
 */
@Entity(
    tableName = "message",
    indices = [
        Index(value = ["msg_id", "user_id"], unique = true),
        Index(value = ["user_id", "other_id"]),
        Index(value = ["recipient"])
    ]
)
data class MessageEntity(
    /**
     * 数据库主键
     */
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "table_id")
    val tableId: Int? = null,

    /**
     * 当前消息类型
     */
    @ColumnInfo(name = "command")
    val command: String,
    /**
     * 登录用户id
     */
    @ColumnInfo(name = "user_id")
    val userId: Long,
    /**
     * 对方用户id
     */
    @ColumnInfo(name = "other_id")
    val otherId: Long,
    /**
     * 发送方id
     */
    @ColumnInfo(name = "sender")
    val sender: Long,
    /**
     * 接收方id
     */
    @ColumnInfo(name = "recipient")
    val recipient: Long,
    /**
     * 当前消息内容类型
     */
    @ColumnInfo(name = "type")
    val type: Long,
    /**
     * 当前消息内容  存的json形式   根据type去解析即可
     */
    @ColumnInfo(name = "content")
    val content: String,
    /**
     * 当前消息状态   例如 成功  失败  发送中
     */
    @ColumnInfo(name = "state")
    val state: Int,
    /**
     * 消息id
     */
    @ColumnInfo(name = "msg_id")
    val msgId: String,
    /**
     * timestamp时间戳
     */
    @ColumnInfo(name = "timestamp")
    var timestamp: Long,

    /**
     * seqId 后端会传过来，有些地方后端需要
     */
    @ColumnInfo(name = "seq_id")
    val seqId: Long? = null,
    /**
     * 消息扩展  根据后续的业务可定
     */
    @ColumnInfo(name = "ext")
    val ext: String? = null,
    /**
     * 字段也是一个Json字符串，里面会存一些服务端填充和下发的东西
     */
    @ColumnInfo(name = "payload")
    val payload: String? = null,
    /**
     * 字段也是一个Json字符串，额外的信息
     */
    @ColumnInfo(name = "parameter")
    val parameter: String? = null,
    /**
     * 翻译过后的文本
     */
    @ColumnInfo(name = "translate_content")
    val translateContent: String? = null,
    //---------- 只有自己发送有
    /**
     * 与对方的聊天等级
     */
    @ColumnInfo(name = "hot_level")
    val hotLevel: Int? = null,
    /**
     * 异常消息，如果有则说明发生异常，未正常发送
     * 这个异常是系统主动返回，而不是发生真实的异常，需要与[errorMessage]结合使用
     */
    @ColumnInfo(name = "resp_error_message")
    val respErrorMessage: String? = null,
    /**
     * 重试过期时间，如果为空说明不需要重试，如果不为空则说明需要重试并且在时间到期后不允许重试
     */
    @ColumnInfo(name = "retry_expired_time")
    val retryExpiredTime: Long? = null,
    /**
     * 异常码
     */
    @ColumnInfo(name = "error_code")
    val errorCode: Int? = null,
    /**
     * 异常信息
     */
    @ColumnInfo(name = "error_message")
    val errorMessage: String? = null,
) {

    @delegate:Ignore
    val extBean: SingleMessageExtBean? by lazy {
        if (ext.isNullOrEmpty()) {
            SingleMessageExtBean()
        } else {
            JsonUtils.fromJson<SingleMessageExtBean>(ext)
        }
    }

    @delegate:Ignore
    val payloadBean: SingleMessagePayloadBean? by lazy {
        if (payload.isNullOrEmpty()) {
            SingleMessagePayloadBean()
        } else {
            JsonUtils.fromJson<SingleMessagePayloadBean>(payload)
        }
    }

    @delegate:Ignore
    val parameterBean: SingleMessageParameterBean? by lazy {
        if (parameter.isNullOrEmpty()) {
            SingleMessageParameterBean()
        } else {
            JsonUtils.fromJson<SingleMessageParameterBean>(parameter)
        }
    }

}